package org.jeecg.modules.school.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.area.service.AreaService;
import org.jeecg.modules.school.entity.School;
import org.jeecg.modules.school.entity.vo.QueryVo;
import org.jeecg.modules.school.mapper.SciSchoolMapper;
import org.jeecg.modules.school.service.SciSchoolService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liujiamiao
 * @since 2020-11-18
 */
@Service
public class SciSchoolServiceImpl extends ServiceImpl<SciSchoolMapper, School> implements SciSchoolService {

    @Autowired
    private AreaService areaService;

    /**
     * 获取学校
     * @param page
     * @param size
     * @param queryVo
     * @return
     */
    @Override
    public IPage<School> queryPage(Integer page, Integer size, QueryVo queryVo) {

        Page<School> schoolPage = new Page<>(page, size);
        QueryWrapper wrapper = new QueryWrapper();

        if(queryVo!=null){
            //所在城市（传的是城市，数据库里是编码
            if(!StringUtils.isEmpty(queryVo.getCity())){
                String cityCode = areaService.findCityCodeByCityName(queryVo.getCity());
                wrapper.eq("ssl_city",cityCode);
            }
            //高校编码
            if(!StringUtils.isEmpty(queryVo.getCode())){
                wrapper.like("ssl_code",queryVo.getCode());
            }
            //学校名称
            if(!StringUtils.isEmpty(queryVo.getName())){
                wrapper.like("ssl_name",queryVo.getName());
            }
        }

        wrapper.orderByAsc("ssl_level");

        IPage ipage = this.page(schoolPage, wrapper);
        return ipage;
    }

    /**
     * 返回学校名称
     * @param schoolCode
     * @return
     */
    @Override
    public String querySchoolName(String schoolCode) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("ssl_code",schoolCode);
        School school = this.getOne(wrapper);
        if(school==null){
            return null;
        }
        return school.getSslName();
    }

    /**
     * 学校名称获取学校编码
     * @param schoolName
     * @return
     */
    @Override
    public String querySchoolCode(String schoolName) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("ssl_name",schoolName);
        School school = this.getOne(wrapper);
        if(school==null){
            return null;
        }
        return school.getSslCode();
    }

    public String readJson() throws IOException {
        String path = this.getClass().getClassLoader().getResource("/Test.json").getPath().replace("%20", " ");
        File file = new File(path);
        FileReader fileReader = new FileReader(file);
        Reader reader = new InputStreamReader(new FileInputStream(file),"utf-8");
        int ch = 0;
        StringBuffer sb = new StringBuffer();
        while ((ch = reader.read()) != -1) {
            sb.append((char) ch);
        }
        fileReader.close();
        reader.close();

        return sb.toString();
    }

}
